<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="format-detection" content="telephone=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><link rel="icon" href="/images/icons/favicon-16x16.png?v=2.6.2" type="image/png" sizes="16x16"><link rel="icon" href="/images/icons/favicon-32x32.png?v=2.6.2" type="image/png" sizes="32x32"><meta name="description" content="AWT中常用组件                           基本组件">
<meta property="og:type" content="article">
<meta property="og:title" content="java-GUI编程之AWT组件">
<meta property="og:url" content="https://kohler19.gitee.io/2022/04/05/java-GUI2/index.html">
<meta property="og:site_name" content="愷龍的网络日志">
<meta property="og:description" content="AWT中常用组件                           基本组件">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://pic.imgdb.cn/item/624bcb8d239250f7c50e7631.jpg">
<meta property="og:image" content="https://pic.imgdb.cn/item/624bcd01239250f7c5114d7d.jpg">
<meta property="og:image" content="https://pic.imgdb.cn/item/624bd28c239250f7c51d67d5.jpg">
<meta property="og:image" content="https://pic.imgdb.cn/item/624bd20c239250f7c51c5bd7.jpg">
<meta property="og:image" content="https://s4.ax1x.com/2022/01/03/Tb8ZB4.png">
<meta property="article:published_time" content="2022-04-05T02:39:40.000Z">
<meta property="article:modified_time" content="2022-04-05T05:27:02.761Z">
<meta property="article:author" content="李恺龙">
<meta property="article:tag" content="java">
<meta property="article:tag" content="GUI">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://pic.imgdb.cn/item/624bcb8d239250f7c50e7631.jpg"><title>java-GUI编程之AWT组件 | 愷龍的网络日志</title><link ref="canonical" href="https://kohler19.gitee.io/2022/04/05/java-GUI2/"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.12.1/css/all.min.css" type="text/css"><link rel="stylesheet" href="/css/index.css?v=2.6.2"><script>var Stun = window.Stun || {};
var CONFIG = {
  root: '/',
  algolia: undefined,
  assistSearch: undefined,
  fontIcon: {"prompt":{"success":"fas fa-check-circle","info":"fas fa-arrow-circle-right","warning":"fas fa-exclamation-circle","error":"fas fa-times-circle"},"copyBtn":"fas fa-copy"},
  sidebar: {"offsetTop":"20px","tocMaxDepth":6},
  header: {"enable":true,"showOnPost":true,"scrollDownIcon":false},
  postWidget: {"endText":true},
  nightMode: {"enable":true},
  back2top: {"enable":true},
  codeblock: {"style":"default","highlight":"light","wordWrap":false},
  reward: false,
  fancybox: false,
  zoomImage: {"gapAside":"20px"},
  galleryWaterfall: undefined,
  lazyload: false,
  pjax: undefined,
  externalLink: {"icon":{"enable":true,"name":"fas fa-external-link-alt"}},
  shortcuts: undefined,
  prompt: {"copyButton":"复制","copySuccess":"复制成功","copyError":"复制失败"},
  sourcePath: {"js":"js","css":"css","images":"images"},
};

window.CONFIG = CONFIG;</script><meta name="generator" content="Hexo 5.4.0"></head><body><div class="container" id="container"><header class="header" id="header"><div class="header-inner"><nav class="header-nav header-nav--fixed"><div class="header-nav-inner"><div class="header-nav-menubtn"><i class="fas fa-bars"></i></div><div class="header-nav-menu"><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/"><span class="header-nav-menu-item__icon"><i class="fas fa-home"></i></span><span class="header-nav-menu-item__text">首页</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/archives/"><span class="header-nav-menu-item__icon"><i class="fas fa-folder-open"></i></span><span class="header-nav-menu-item__text">归档</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/分类/"><span class="header-nav-menu-item__icon"><i class="fas fa-layer-group"></i></span><span class="header-nav-menu-item__text">分类</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/标签/"><span class="header-nav-menu-item__icon"><i class="fas fa-tags"></i></span><span class="header-nav-menu-item__text">标签</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/categories/Book/"><span class="header-nav-menu-item__icon"><i class="fas fa-book"></i></span><span class="header-nav-menu-item__text">书籍</span></a></div></div><div class="header-nav-search"><span class="header-nav-search__icon"><i class="fas fa-search"></i></span><span class="header-nav-search__text">搜索</span></div><div class="header-nav-mode"><div class="mode"><div class="mode-track"><span class="mode-track-moon"></span><span class="mode-track-sun"></span></div><div class="mode-thumb"></div></div></div></div></nav><div class="header-banner"><div class="header-banner-info"><div class="header-banner-info__title">愷龍的网络日志</div><div class="header-banner-info__subtitle">每天多学一点，以后就少敲一点代码</div></div></div></div></header><main class="main" id="main"><div class="main-inner"><div class="content-wrap" id="content-wrap"><div class="content" id="content"><!-- Just used to judge whether it is an article page--><div id="is-post"></div><div class="post"><header class="post-header"><h1 class="post-title">java-GUI编程之AWT组件</h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2022-04-05</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2022-04-05</span></span></div></header><div class="post-body">
        <h1 id="AWT中常用组件"   >
          <a href="#AWT中常用组件" class="heading-link"><i class="fas fa-link"></i></a><a href="#AWT中常用组件" class="headerlink" title="AWT中常用组件"></a>AWT中常用组件</h1>
      
        <h2 id="基本组件"   >
          <a href="#基本组件" class="heading-link"><i class="fas fa-link"></i></a><a href="#基本组件" class="headerlink" title="基本组件"></a>基本组件</h2>
      <span id="more"></span>

<div class="table-container"><table>
<thead>
<tr>
<th>组件名</th>
<th>功能</th>
</tr>
</thead>
<tbody><tr>
<td>Button</td>
<td>Button</td>
</tr>
<tr>
<td>Canvas</td>
<td>用于绘图的画布</td>
</tr>
<tr>
<td>Checkbox</td>
<td>复选框组件（也可当做单选框组件使用）</td>
</tr>
<tr>
<td>CheckboxGroup</td>
<td>用于将多个Checkbox 组件组合成一组， 一组 Checkbox 组件将只有一个可以 被选中 ， 即全部变成单选框组件</td>
</tr>
<tr>
<td>Choice</td>
<td>下拉选择框</td>
</tr>
<tr>
<td>Frame</td>
<td>窗口 ， 在 GUI 程序里通过该类创建窗口</td>
</tr>
<tr>
<td>Label</td>
<td>标签类，用于放置提示性文本</td>
</tr>
<tr>
<td>List</td>
<td>JU表框组件，可以添加多项条目</td>
</tr>
<tr>
<td>Panel</td>
<td>不能单独存在基本容器类，必须放到其他容器中</td>
</tr>
<tr>
<td>Scrollbar</td>
<td>滑动条组件。如果需要用户输入位于某个范围的值 ， 就可以使用滑动条组件 ，比如调 色板中设置 RGB 的三个值所用的滑动条。当创建一个滑动条时，必须指定它的方向、初始值、 滑块的大小、最小值和最大值。</td>
</tr>
<tr>
<td>ScrollPane</td>
<td>带水平及垂直滚动条的容器组件</td>
</tr>
<tr>
<td>TextArea</td>
<td>多行文本域</td>
</tr>
<tr>
<td>TextField</td>
<td>单行文本框</td>
</tr>
</tbody></table></div>
<p>这些 AWT 组件的用法比较简单，可以查阅 API 文档来获取它们各自的构方法、成员方法等详细信息。</p>
<blockquote>
<p>API 文档地址：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://www.apiref.com/java11-zh/java.desktop/javax/swing/package-summary.html" >https://www.apiref.com/java11-zh/java.desktop/javax/swing/package-summary.html</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span></p>
</blockquote>
<p><strong>案例：</strong> </p>
<p>​    实现下图效果：</p>
<p>​<center><br><img src="https://pic.imgdb.cn/item/624bcb8d239250f7c50e7631.jpg"></p>
</center>

<p><strong>演示代码：</strong></p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> javax.swing.*;<br><span class="hljs-keyword">import</span> java.awt.*;<br><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BasicComponentDemo</span> </span>&#123;<br>    Frame frame = <span class="hljs-keyword">new</span> Frame(<span class="hljs-string">&quot;这里测试基本组件&quot;</span>);<br><br>    <span class="hljs-comment">//定义一个按钮</span><br>    Button ok = <span class="hljs-keyword">new</span> Button(<span class="hljs-string">&quot;确认&quot;</span>);<br><br>    <span class="hljs-comment">//定义一个复选框组</span><br>    CheckboxGroup cbg = <span class="hljs-keyword">new</span> CheckboxGroup();<br>    <span class="hljs-comment">//定义一个单选框，初始处于被选中状态,并添加到cbg组中</span><br>    Checkbox male = <span class="hljs-keyword">new</span> Checkbox(<span class="hljs-string">&quot;男&quot;</span>, cbg, <span class="hljs-keyword">true</span>);<br><br>    <span class="hljs-comment">//定义一个单选框，初始处于未被选中状态,并添加到cbg组中</span><br>    Checkbox female = <span class="hljs-keyword">new</span> Checkbox(<span class="hljs-string">&quot;女&quot;</span>, cbg, <span class="hljs-keyword">false</span>);<br><br>    <span class="hljs-comment">//定义一个复选框，初始处于未被选中状态</span><br>    Checkbox married = <span class="hljs-keyword">new</span> Checkbox(<span class="hljs-string">&quot;是否已婚？&quot;</span>, <span class="hljs-keyword">false</span>);<br><br>    <span class="hljs-comment">//定义一个下拉选择框</span><br>    Choice colorChooser = <span class="hljs-keyword">new</span> Choice();<br><br>    <span class="hljs-comment">//定义一个列表选择框</span><br>    List colorList = <span class="hljs-keyword">new</span> List(<span class="hljs-number">6</span>, <span class="hljs-keyword">true</span>);<br><br>    <span class="hljs-comment">//定义一个5行，20列的多行文本域</span><br>    TextArea ta = <span class="hljs-keyword">new</span> TextArea(<span class="hljs-number">5</span>, <span class="hljs-number">20</span>);<br><br>    <span class="hljs-comment">//定义一个50列的单行文本域</span><br>    TextField tf = <span class="hljs-keyword">new</span> TextField(<span class="hljs-number">50</span>);<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">init</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-comment">//往下拉选择框中添加内容</span><br>        colorChooser.add(<span class="hljs-string">&quot;红色&quot;</span>);<br>        colorChooser.add(<span class="hljs-string">&quot;绿色&quot;</span>);<br>        colorChooser.add(<span class="hljs-string">&quot;蓝色&quot;</span>);<br><br>        <span class="hljs-comment">//往列表选择框中添加内容</span><br>        colorList.add(<span class="hljs-string">&quot;红色&quot;</span>);<br>        colorList.add(<span class="hljs-string">&quot;绿色&quot;</span>);<br>        colorList.add(<span class="hljs-string">&quot;蓝色&quot;</span>);<br><br>        <span class="hljs-comment">//创建一个装载按钮和文本框的Panel容器</span><br>        Panel bottom = <span class="hljs-keyword">new</span> Panel();<br>        bottom.add(tf);<br>        bottom.add(ok);<br><br>        <span class="hljs-comment">//把bottom添加到Frame的底部</span><br>        frame.add(bottom,BorderLayout.SOUTH);<br><br>        <span class="hljs-comment">//创建一个Panel容器，装载下拉选择框，单选框和复选框</span><br>        Panel checkPanel = <span class="hljs-keyword">new</span> Panel();<br>        checkPanel.add(colorChooser);<br>        checkPanel.add(male);<br>        checkPanel.add(female);<br>        checkPanel.add(married);<br><br>        <span class="hljs-comment">//创建一个垂直排列的Box容器，装载 多行文本域和checkPanel</span><br>        Box topLeft = Box.createVerticalBox();<br>        topLeft.add(ta);<br>        topLeft.add(checkPanel);<br><br>        <span class="hljs-comment">//创建一个水平排列的Box容器，装载topLeft和列表选择框</span><br>        Box top = Box.createHorizontalBox();<br>        top.add(topLeft);<br>        top.add(colorList);<br><br>        <span class="hljs-comment">//将top添加到frame的中间区域</span><br>        frame.add(top);<br><br><br>        <span class="hljs-comment">//设置frame最佳大小并可见</span><br>        frame.pack();<br>        frame.setVisible(<span class="hljs-keyword">true</span>);<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br><br>        <span class="hljs-keyword">new</span> BasicComponentDemo().init();<br><br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></div></figure>


        <h2 id="对话框Dialog"   >
          <a href="#对话框Dialog" class="heading-link"><i class="fas fa-link"></i></a><a href="#对话框Dialog" class="headerlink" title="对话框Dialog"></a>对话框Dialog</h2>
      
        <h3 id="Dialog"   >
          <a href="#Dialog" class="heading-link"><i class="fas fa-link"></i></a><a href="#Dialog" class="headerlink" title="Dialog"></a>Dialog</h3>
      <p>Dialog 是 Window 类的子类，是 一个容器类，属于特殊组件 。 对话框是可以独立存在的顶级窗口， 因此用法与普通窗口的用法几乎完全一样，但是使用对话框需要注意下面两点：</p>
<ul>
<li>对话框通常依赖于其他窗口，就是通常需要有一个父窗口；</li>
<li>对话框有非模式(non-modal)和模式(modal)两种，当某个模式对话框被打开后，该模式对话框总是位于它的父窗口之上，在模式对话框被关闭之前，父窗口无法获得焦点。</li>
</ul>
<blockquote>
<p>模式窗体：你必须关闭该窗体，才能操作其它窗体；比如说，必须按确定或取消，或者按关闭。<br>非模式窗体：不必关闭该窗体，就可转换到其它窗体上进行操作。 </p>
</blockquote>
<div class="table-container"><table>
<thead>
<tr>
<th>方法名称</th>
<th>方法功能</th>
</tr>
</thead>
<tbody><tr>
<td>Dialog(Frame owner, String title, boolean modal)</td>
<td>创建一个对话框对象：<br/>owner:当前对话框的父窗口<br/>title:当前对话框的标题<br/>modal：当前对话框是否是模式对话框，true/false</td>
</tr>
</tbody></table></div>
<p><strong>案例1：</strong></p>
<p>​    通过Frame、Button、Dialog实现下图效果:</p>
<center>
<img src="https://pic.imgdb.cn/item/624bcd01239250f7c5114d7d.jpg">
</center>

<p><strong>演示代码1：</strong></p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.awt.event.ActionEvent;<br><span class="hljs-keyword">import</span> java.awt.event.ActionListener;<br><span class="hljs-keyword">import</span> java.awt.*;<br><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DialogDemo1</span> </span>&#123;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br><br>        Frame frame = <span class="hljs-keyword">new</span> Frame(<span class="hljs-string">&quot;这里测试Dialog&quot;</span>);<br><br>        Dialog d1 = <span class="hljs-keyword">new</span> Dialog(frame, <span class="hljs-string">&quot;模式对话框&quot;</span>, <span class="hljs-keyword">true</span>);<br>        Dialog d2 = <span class="hljs-keyword">new</span> Dialog(frame, <span class="hljs-string">&quot;非模式对话框&quot;</span>, <span class="hljs-keyword">false</span>);<br><br>        Button b1 = <span class="hljs-keyword">new</span> Button(<span class="hljs-string">&quot;打开模式对话框&quot;</span>);<br>        Button b2 = <span class="hljs-keyword">new</span> Button(<span class="hljs-string">&quot;打开非模式对话框&quot;</span>);<br><br>        <span class="hljs-comment">//设置对话框的大小和位置</span><br>        d1.setBounds(<span class="hljs-number">20</span>,<span class="hljs-number">30</span>,<span class="hljs-number">300</span>,<span class="hljs-number">400</span>);<br>        d2.setBounds(<span class="hljs-number">20</span>,<span class="hljs-number">30</span>,<span class="hljs-number">300</span>,<span class="hljs-number">400</span>);<br><br>        <span class="hljs-comment">//给b1和b2绑定监听事件</span><br>        b1.addActionListener(<span class="hljs-keyword">new</span> ActionListener() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">actionPerformed</span><span class="hljs-params">(ActionEvent e)</span> </span>&#123;<br>                d1.setVisible(<span class="hljs-keyword">true</span>);<br>            &#125;<br>        &#125;);<br>        b2.addActionListener(<span class="hljs-keyword">new</span> ActionListener() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">actionPerformed</span><span class="hljs-params">(ActionEvent e)</span> </span>&#123;<br>                d2.setVisible(<span class="hljs-keyword">true</span>);<br>            &#125;<br>        &#125;);<br><br>        <span class="hljs-comment">//把按钮添加到frame中</span><br>        frame.add(b1);<br>        frame.add(b2,BorderLayout.SOUTH);<br><br>        <span class="hljs-comment">//设置frame最佳大小并可见</span><br>        frame.pack();<br>        frame.setVisible(<span class="hljs-keyword">true</span>);<br><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></div></figure>

<p>在Dialog对话框中，可以根据需求，自定义内容</p>
<p><strong>案例：</strong></p>
<p>​    点击按钮，弹出一个模式对话框，其内容如下:</p>
<center>
<img src="https://pic.imgdb.cn/item/624bd28c239250f7c51d67d5.jpg">
</center>

<p><strong>演示代码：</strong></p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.awt.event.ActionEvent;<br><span class="hljs-keyword">import</span> java.awt.event.ActionListener;<br><span class="hljs-keyword">import</span> javax.swing.Box;<br><span class="hljs-keyword">import</span> javax.swing.JButton;<br><span class="hljs-keyword">import</span> java.awt.*;<br><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DialogDemo2</span> </span>&#123;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br><br>        Frame frame = <span class="hljs-keyword">new</span> Frame(<span class="hljs-string">&quot;这里测试Dialog&quot;</span>);<br><br>        Dialog d1 = <span class="hljs-keyword">new</span> Dialog(frame, <span class="hljs-string">&quot;模式对话框&quot;</span>, <span class="hljs-keyword">true</span>);<br><br>        <span class="hljs-comment">//往对话框中添加内容</span><br>        Box vBox = Box.createVerticalBox();<br><br>        vBox.add(<span class="hljs-keyword">new</span> TextField(<span class="hljs-number">15</span>));<br>        vBox.add(<span class="hljs-keyword">new</span> JButton(<span class="hljs-string">&quot;确认&quot;</span>));<br>        d1.add(vBox);<br><br>        Button b1 = <span class="hljs-keyword">new</span> Button(<span class="hljs-string">&quot;打开模式对话框&quot;</span>);<br><br>        <span class="hljs-comment">//设置对话框的大小和位置</span><br>        d1.setBounds(<span class="hljs-number">20</span>,<span class="hljs-number">30</span>,<span class="hljs-number">200</span>,<span class="hljs-number">100</span>);<br><br><br>        <span class="hljs-comment">//给b1绑定监听事件</span><br>        b1.addActionListener(<span class="hljs-keyword">new</span> ActionListener() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">actionPerformed</span><span class="hljs-params">(ActionEvent e)</span> </span>&#123;<br>                d1.setVisible(<span class="hljs-keyword">true</span>);<br>            &#125;<br>        &#125;);<br><br><br>        <span class="hljs-comment">//把按钮添加到frame中</span><br>        frame.add(b1);<br><br>        <span class="hljs-comment">//设置frame最佳大小并可见</span><br>        frame.pack();<br>        frame.setVisible(<span class="hljs-keyword">true</span>);<br><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></div></figure>


        <h3 id="FileDialog"   >
          <a href="#FileDialog" class="heading-link"><i class="fas fa-link"></i></a><a href="#FileDialog" class="headerlink" title="FileDialog"></a>FileDialog</h3>
      <p>Dialog 类还有 一个子类 : FileDialog ，它代表一个文件对话框，用于打开或者保存 文件,需要注意的是FileDialog无法指定模态或者非模态，这是因为 FileDialog 依赖于运行平台的实现，如果运行平台的文件对话框是模态的，那么 FileDialog 也是模态的;否则就是非模态的 。</p>
<div class="table-container"><table>
<thead>
<tr>
<th>方法名称</th>
<th>方法功能</th>
</tr>
</thead>
<tbody><tr>
<td>FileDialog(Frame parent, String title, int mode)</td>
<td>创建一个文件对话框：<br/>parent:指定父窗口<br/>title:对话框标题<br/>mode:文件对话框类型，如果指定为FileDialog.load，用于打开文件，如果指定为FileDialog.SAVE,用于保存文件</td>
</tr>
<tr>
<td>String getDirectory()</td>
<td>获取被打开或保存文件的绝对路径</td>
</tr>
<tr>
<td>String getFile()</td>
<td>获取被打开或保存文件的文件名</td>
</tr>
</tbody></table></div>
<p><strong>案例2：</strong></p>
<p>​    使用 Frame、Button和FileDialog完成下图效果：</p>
<center>
<img src="https://pic.imgdb.cn/item/624bd20c239250f7c51c5bd7.jpg">
</center>



<p><strong>演示代码2：</strong></p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.awt.*;<br><span class="hljs-keyword">import</span> java.awt.event.ActionEvent;<br><span class="hljs-keyword">import</span> java.awt.event.ActionListener;<br><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FileDialogTest</span> </span>&#123;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br><br>        Frame frame = <span class="hljs-keyword">new</span> Frame(<span class="hljs-string">&quot;这里测试FileDialog&quot;</span>);<br><br>        FileDialog d1 = <span class="hljs-keyword">new</span> FileDialog(frame, <span class="hljs-string">&quot;选择需要加载的文件&quot;</span>, FileDialog.LOAD);<br>        FileDialog d2 = <span class="hljs-keyword">new</span> FileDialog(frame, <span class="hljs-string">&quot;选择需要保存的文件&quot;</span>, FileDialog.SAVE);<br><br>        Button b1 = <span class="hljs-keyword">new</span> Button(<span class="hljs-string">&quot;打开文件&quot;</span>);<br>        Button b2 = <span class="hljs-keyword">new</span> Button(<span class="hljs-string">&quot;保存文件&quot;</span>);<br><br>        <span class="hljs-comment">//给按钮添加事件</span><br>        b1.addActionListener(<span class="hljs-keyword">new</span> ActionListener() &#123;<br>            <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">actionPerformed</span><span class="hljs-params">(ActionEvent e)</span> </span>&#123;<br>                d1.setVisible(<span class="hljs-keyword">true</span>);<br>                <span class="hljs-comment">//打印用户选择的文件路径和名称</span><br>                System.out.println(<span class="hljs-string">&quot;用户选择的文件路径:&quot;</span>+d1.getDirectory());<br>                System.out.println(<span class="hljs-string">&quot;用户选择的文件名称:&quot;</span>+d1.getFile());<br>            &#125;<br>        &#125;);<br><br>        System.out.println(<span class="hljs-string">&quot;-------------------------------&quot;</span>);<br>        b2.addActionListener(<span class="hljs-keyword">new</span> ActionListener() &#123;<br>            <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">actionPerformed</span><span class="hljs-params">(ActionEvent e)</span> </span>&#123;<br>                d2.setVisible(<span class="hljs-keyword">true</span>);<br>                <span class="hljs-comment">//打印用户选择的文件路径和名称</span><br>                System.out.println(<span class="hljs-string">&quot;用户选择的文件路径:&quot;</span>+d2.getDirectory());<br>                System.out.println(<span class="hljs-string">&quot;用户选择的文件名称:&quot;</span>+d2.getFile());<br>            &#125;<br>        &#125;);<br><br>        <span class="hljs-comment">//添加按钮到frame中</span><br><br>        frame.add(b1);<br>        frame.add(b2,BorderLayout.SOUTH);<br><br>        <span class="hljs-comment">//设置frame最佳大小并可见</span><br>        frame.pack();<br>        frame.setVisible(<span class="hljs-keyword">true</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></div></figure>

<center>如果您有什么问题或建议可以在下方的评论区评论，我会及时回复的。<center>
<center><font color="red">欢迎关注我的公众号，共同学习，共同提升！</font></center>
<center><font color="red">您可以通过公众号向我留言，也可以通过邮箱（lklong@88.com）联系我</font></center>
<center>
    <img src="https://s4.ax1x.com/2022/01/03/Tb8ZB4.png">
</center></div><footer class="post-footer"><div class="post-ending ending"><div class="ending__text">------ 本文结束，感谢您的阅读 ------</div></div><div class="post-copyright copyright"><div class="copyright-author"><span class="copyright-author__name">本文作者: </span><span class="copyright-author__value"><a href="https://kohler19.gitee.io">李恺龙</a></span></div><div class="copyright-link"><span class="copyright-link__name">本文链接: </span><span class="copyright-link__value"><a href="https://kohler19.gitee.io/2022/04/05/java-GUI2/">https://kohler19.gitee.io/2022/04/05/java-GUI2/</a></span></div><div class="copyright-notice"><span class="copyright-notice__name">版权声明: </span><span class="copyright-notice__value">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" rel="external nofollow" target="_blank">BY-NC-SA</a> 许可协议。转载请注明出处！</span></div></div><div class="post-tags"><span class="post-tags-item"><span class="post-tags-item__icon"><i class="fas fa-tag"></i></span><a class="post-tags-item__link" href="https://kohler19.gitee.io/tags/java/">java</a></span><span class="post-tags-item"><span class="post-tags-item__icon"><i class="fas fa-tag"></i></span><a class="post-tags-item__link" href="https://kohler19.gitee.io/tags/GUI/">GUI</a></span></div><nav class="post-paginator paginator"><div class="paginator-prev"><a class="paginator-prev__link" href="/2022/04/06/java-GUI3/"><span class="paginator-prev__icon"><i class="fas fa-angle-left"></i></span><span class="paginator-prev__text">java-GUI编程之事件处理</span></a></div><div class="paginator-next"><a class="paginator-next__link" href="/2022/04/02/java-GUI1/"><span class="paginator-prev__text">java-GUI编程之布局类型介绍</span><span class="paginator-next__icon"><i class="fas fa-angle-right"></i></span></a></div></nav></footer></div></div><div class="comments" id="comments"><div id="valine-container"></div></div></div><div class="sidebar-wrap" id="sidebar-wrap"><aside class="sidebar" id="sidebar"><div class="sidebar-nav"><span class="sidebar-nav-toc current">文章目录</span><span class="sidebar-nav-ov">站点概览</span></div><section class="sidebar-toc"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#AWT%E4%B8%AD%E5%B8%B8%E7%94%A8%E7%BB%84%E4%BB%B6"><span class="toc-number">1.</span> <span class="toc-text">
          AWT中常用组件</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E7%BB%84%E4%BB%B6"><span class="toc-number">1.1.</span> <span class="toc-text">
          基本组件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AF%B9%E8%AF%9D%E6%A1%86Dialog"><span class="toc-number">1.2.</span> <span class="toc-text">
          对话框Dialog</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Dialog"><span class="toc-number">1.2.1.</span> <span class="toc-text">
          Dialog</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#FileDialog"><span class="toc-number">1.2.2.</span> <span class="toc-text">
          FileDialog</span></a></li></ol></li></ol></li></ol></section><!-- ov = overview--><section class="sidebar-ov hide"><div class="sidebar-ov-author"><div class="sidebar-ov-author__avatar"><img class="sidebar-ov-author__avatar_img" src="https://s4.ax1x.com/2022/01/03/Tb2VW6.png" alt="avatar"></div><p class="sidebar-ov-author__text">格物致知，知行合一</p></div><div class="sidebar-ov-social"><a class="sidebar-ov-social-item" href="https://s4.ax1x.com/2022/01/03/Tb8ZB4.png" target="_blank" rel="noopener" data-popover="微信" data-popover-pos="up"><span class="sidebar-ov-social-item__icon"><i class="fab fa-weixin"></i></span></a><a class="sidebar-ov-social-item" href="mailto:lklong@88.com" target="_blank" rel="noopener" data-popover="social.Email" data-popover-pos="up"><span class="sidebar-ov-social-item__icon">lklong@88.com</span></a></div><div class="sidebar-ov-cc"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" target="_blank" rel="noopener" data-popover="知识共享许可协议" data-popover-pos="up"><img src="/images/cc-by-nc-sa.svg"></a></div></section><div class="sidebar-reading"><div class="sidebar-reading-info"><span class="sidebar-reading-info__text">你已阅读了 </span><span class="sidebar-reading-info__num">0</span><span class="sidebar-reading-info__perc">%</span></div><div class="sidebar-reading-line"></div></div></aside></div><div class="clearfix"></div></div></main><footer class="footer" id="footer"><div class="footer-inner"><div><span>Copyright © 2023</span><span class="footer__icon"><i class="fas fa-heart"></i></span><span>愷龍 All Rights Reserved</span></div><div><span>由 <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a> 强力驱动</span><span> v5.4.0</span><span class="footer__devider">|</span><span>主题 - <a href="https://github.com/liuyib/hexo-theme-stun/" title="Stun" target="_blank" rel="noopener">Stun</a></span><span> v2.6.2</span></div></div></footer><div class="loading-bar" id="loading-bar"><div class="loading-bar__progress"></div></div><div class="back2top" id="back2top"><span class="back2top__icon"><i class="fas fa-rocket"></i></span></div></div><div class="search-mask"></div><div class="search-popup"><span class="search-close"></span><div class="search-input"><input placeholder="搜索文章（支持多关键词，请用空格分隔）"></div><div class="search-results"></div></div><script src="https://cdn.jsdelivr.net/npm/jquery@v3.4.1/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.ui.min.js"></script><script>function initSearch() {
  var isXML = true;
  var search_path = 'search.xml';

  if (!search_path) {
    search_path = 'search.xml';
  } else if (/json$/i.test(search_path)) {
    isXML = false;
  }

  var path = '/' + search_path;
  $.ajax({
    url: path,
    dataType: isXML ? 'xml' : 'json',
    async: true,
    success: function (res) {
      var datas = isXML ? $('entry', res).map(function () {
        // 将 XML 转为 JSON
        return {
          title: $('title', this).text(),
          content: $('content', this).text(),
          url: $('url', this).text()
        };
      }).get() : res;
      var $input = $('.search-input input');
      var $result = $('.search-results');
      // 搜索对象（标题、内容）的权重，影响显示顺序
      var WEIGHT = { title: 100, content: 1 };
      var searchPost = function () {
        var searchText = $input.val().toLowerCase().trim();
        // 根据空白字符分隔关键字
        var keywords = searchText.split(/[\s]+/);
        // 搜索结果
        var matchPosts = [];

        // 有多个关键字时，将原文字整个保存下来
        if (keywords.length > 1) {
          keywords.push(searchText);
        }
        // 防止未输入字符时搜索
        if (searchText.length > 0) {
          datas.forEach(function (data) {
            var isMatch  = false;
            // 没有标题的文章使用预设的 i18n 变量代替
            var title = (data.title && data.title.trim()) || '[ 文章无标题 ]';
            var titleLower = title && title.toLowerCase();
            // 删除 HTML 标签 和 所有空白字符
            var content = data.content && data.content.replace(/<[^>]+>/g, '');
            var contentLower = content && content.toLowerCase();
            // 删除重复的 /
            var postURL = data.url && decodeURI(data.url).replace(/\/{2,}/g, '/');
            // 标题中匹配到的关键词
            var titleHitSlice = [];
            // 内容中匹配到的关键词
            var contentHitSlice = [];

            keywords.forEach(function (keyword) {
              /**
              * 获取匹配的关键词的索引
              * @param {String} keyword 要匹配的关键字
              * @param {String} text 原文字
              * @param {Boolean} caseSensitive 是否区分大小写
              * @param {Number} weight 匹配对象的权重。权重大的优先显示
              * @return {Array}
              */
              function getIndexByword (word, text, caseSensitive, weight) {
                if (!word || !text) {
                  return [];
                };

                var startIndex = 0; // 每次匹配的开始索引
                var index = -1;     // 匹配到的索引值
                var result = [];    // 匹配结果

                if (!caseSensitive) {
                  word = word.toLowerCase();
                  text = text.toLowerCase();
                }

                while((index = text.indexOf(word, startIndex)) !== -1) {
                  var hasMatch = false;
                  // 索引位置相同的关键词，保留长度较长的
                  titleHitSlice.forEach(function (hit) {
                    if (hit.index === index && hit.word.length < word.length) {
                      hit.word = word;
                      hasMatch = true;
                    }
                  });
                  startIndex = index + word.length;
                  !hasMatch && result.push({ index: index, word: word, weight: weight });
                }
                return result;
              }
              titleHitSlice = titleHitSlice.concat(getIndexByword(keyword, titleLower, false, WEIGHT.title));
              contentHitSlice = contentHitSlice.concat(getIndexByword(keyword, contentLower, false, WEIGHT.content));
            });

            var hitTitle = titleHitSlice.length;
            var hitContent = contentHitSlice.length;

            if (hitTitle > 0 || hitContent > 0) {
              isMatch = true;
            }
            if (isMatch) {
              ;[titleHitSlice, contentHitSlice].forEach(function (hit) {
                // 按照匹配文字的索引的递增顺序排序
                hit.sort(function (left, right) {
                  return left.index - right.index;
                });
              });
              /**
              * 给文本中匹配到的关键词添加标记，从而进行高亮显示
              * @param {String} text 原文本
              * @param {Array} hitSlice 匹配项的索引信息
              * @param {Number} start 开始索引
              * @param {Number} end 结束索引
              * @return {String}
              */
              function highlightKeyword (text, hitSlice, start, end) {
                if (!text || !hitSlice || !hitSlice.length) {
                  return;
                }

                var result = '';
                var startIndex = start;
                var endIndex = end;
                hitSlice.forEach(function (hit) {
                  if (hit.index < startIndex) {
                    return;
                  }

                  var hitWordEnd = hit.index + hit.word.length;
                  result += text.slice(startIndex, hit.index);
                  result += '<b>' + text.slice(hit.index, hitWordEnd) + '</b>';
                  startIndex = hitWordEnd;
                });
                result += text.slice(startIndex, endIndex);
                return result;
              }

              var postData = {};
              // 文章总的搜索权重
              var postWeight = titleHitSlice.length * WEIGHT.title + contentHitSlice.length * WEIGHT.content;
              // 标记匹配关键词后的标题
              var postTitle = highlightKeyword(title, titleHitSlice, 0, title.length) || title;
              // 标记匹配关键词后的内容
              var postContent;
              // 显示内容的长度
              var SHOW_WORD_LENGTH = 200;
              // 命中关键词前的字符显示长度
              var SHOW_WORD_FRONT_LENGTH = 20;
              var SHOW_WORD_END_LENGTH = SHOW_WORD_LENGTH - SHOW_WORD_FRONT_LENGTH;

              // 截取匹配的第一个字符，前后共 200 个字符来显示
              if (contentHitSlice.length > 0) {
                var firstIndex = contentHitSlice[0].index;
                var start = firstIndex > SHOW_WORD_FRONT_LENGTH ? firstIndex - SHOW_WORD_FRONT_LENGTH : 0;
                var end = firstIndex + SHOW_WORD_END_LENGTH;
                postContent = highlightKeyword(content, contentHitSlice, start, end);
              } else { // 未匹配到内容，直接截取前 200 个字符来显示
                postContent = content.slice(0, SHOW_WORD_LENGTH);
              }
              postData.title = postTitle;
              postData.content = postContent;
              postData.url = postURL;
              postData.weight = postWeight;
              matchPosts.push(postData);
            }
          });
        }

        var resultInnerHtml = '';
        if (matchPosts.length) {
          // 按权重递增的顺序排序，使权重大的优先显示
          matchPosts.sort(function (left, right) {
            return right.weight - left.weight;
          });
          resultInnerHtml += '<ul>';
          matchPosts.forEach(function (post) {
            resultInnerHtml += '<li><a class="search-results-title" href="' + post.url + '">';
            resultInnerHtml += post.title;
            resultInnerHtml += '</a><div class="search-results-content">';
            resultInnerHtml += post.content;
            resultInnerHtml += '</div></li>';
          });
          resultInnerHtml += '</ul>';
        } else {
          resultInnerHtml += '<div class="search-results-none"><i class="far fa-meh"></i></div>';
        }
        $result.html(resultInnerHtml);
      };
      $input.on('input', searchPost);
      $input.on('keyup', function (e) {
        if (e.keyCode === Stun.utils.codeToKeyCode('Enter')) {
          searchPost();
        }
      });
    }
  });
}

function closeSearch () {
  $('body').css({ overflow: 'auto' });
  $('.search-popup').css({ display: 'none' });
  $('.search-mask').css({ display: 'none' });
}

window.addEventListener('DOMContentLoaded', function () {
  Stun.utils.pjaxReloadLocalSearch = function () {
    $('.header-nav-search').on('click', function (e) {
      e.stopPropagation();
      $('body').css('overflow', 'hidden');
      $('.search-popup')
        .velocity('stop')
        .velocity('transition.expandIn', {
          duration: 300,
          complete: function () {
            $('.search-popup input').focus();
          }
        });
      $('.search-mask')
        .velocity('stop')
        .velocity('transition.fadeIn', {
          duration: 300
        });

      initSearch();
    });
    $('.search-mask, .search-close').on('click', function () {
      closeSearch();
    });
    $(document).on('keydown', function (e) {
      // Escape <=> 27
      if (e.keyCode === Stun.utils.codeToKeyCode('Escape')) {
        closeSearch();
      }
    });
  };

  Stun.utils.pjaxReloadLocalSearch();
}, false);

function safeOpenUrl(url) {
  var newTab = window.open();
  newTab.opener = null;
  newTab.location = url;
}

function extSearch(engine) {
  var engines = {
    google: 'https://www.google.com/search?q=',
    bing: 'https://cn.bing.com/search?q=',
    baidu: 'https://www.baidu.com/s?ie=UTF-8&wd=',
  };
  var host = window.location.host;
  var query = $('.search-input input').val().toLowerCase().trim();
  var uri = engines[engine] + query + ' site:' + host;

  if (query) {
    safeOpenUrl(uri);
  } else {
    Stun.utils.popAlert('warning', '请输入字符');
  }
}

var assistSearchList = window.CONFIG.assistSearch;

if (Array.isArray(assistSearchList)) {
  assistSearchList.forEach(function (name) {
    document.querySelector('.search-btns-item--' + name).addEventListener('click', function () {
      extSearch(name);
    }, false);
  });
}</script><script src="https://cdn.jsdelivr.net/npm/leancloud-storage@latest/dist/av-min.js"></script><script src="https://cdn.jsdelivr.net/npm/valine@latest/dist/Valine.min.js"></script><script>function loadValine () {
  var GUEST_INFO = ['nick', 'mail', 'link'];
  var guest_info = 'nick,mail,link';

  guest_info = guest_info.split(',').filter(function(item) {
    return GUEST_INFO.indexOf(item) > -1;
  });
  new Valine({
    el: '#valine-container',
    appId: 'lxsmJYFBR3TcsTpKPFWSN0HX-gzGzoHsz',
    appKey: 'lW6taHRzBHFtJeWSSzXXMAxH',
    notify: true,
    verify: true,
    placeholder: 'Just go go',
    avatar: 'mp',
    meta: guest_info,
    pageSize: '10' || 10,
    visitor: false,
    recordIP: false,
    lang: '' || 'zh-cn',
    path: window.location.pathname
  });
}

if (false) {
  loadValine();
} else {
  window.addEventListener('DOMContentLoaded', loadValine, false);
}</script><script src="/js/utils.js?v=2.6.2"></script><script src="/js/stun-boot.js?v=2.6.2"></script><script src="/js/scroll.js?v=2.6.2"></script><script src="/js/header.js?v=2.6.2"></script><script src="/js/sidebar.js?v=2.6.2"></script><script type="application/json" src="/search.xml"></script></body></html>